// 商品仓库模块
// 引入商品axiosAPI模块
import axios from '../../api/goods.js'
// 仓库状态
const state = {
    goodsList: [], //商品列表
    shopManagerId: "",//门店管理员id
};
// 获取仓库状态
const getters = {
    //获取商品列表
    getGoodsListGetter(state) {
        return state.goodsList
    },
    //获取门店管理员id
    getShopManagerIdGetter(state) {
        return state.shopManagerId
    }
};
//修改仓库状态
const mutations = {
    //刷新商品列表
    getGoodsListMutation(state, pay) {
        state.goodsList = pay;
    },
    //修改门店管理员id
    updateShopManagerIdMutation(state, pay) {
        state.shopManagerId = pay;
    }
};
// 异步修改仓库状态
const actions = {
    //通过axios获取数据库,更新仓库数据
    async getGoodsListAction({ commit }, pay) {
        let obj = { ...pay };
        obj.addition ? delete obj.addition : ""
        return await axios.get(obj).then(res => {
            if (pay) {
                pay.addition === "修改" ? "" : commit("getGoodsListMutation", res.rows)
            } else {
                commit("getGoodsListMutation", res.rows)
            }
            return res;
        })
    },
    //通过axios增加数据库,更新仓库数据
    async postGoodsListAction({ dispatch }, pay) {
        return await axios.post(pay).then(res => dispatch("getGoodsListAction", pay))
    },
    //通过axios修改数据库,更新仓库数据
    async putGoodsListAction({ dispatch }, pay) {
        return await axios.put(pay._id, pay).then(res => dispatch("getGoodsListAction"))
    },
    //通过axios删除数据库,更新仓库数据
    async deleteGoodsListAction({ dispatch }, pay) {
        let obj = { ...pay }
        delete obj.id
        return await axios.delete(pay.id).then(res => dispatch("getGoodsListAction"), obj)
    },
};

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}